import {request} from "../../request/index"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: [],
    isFocus: false
  },
  TimeId: -1,
  inValue: '',

  handleInput(e) {
    // console.log(e);
    let {value} = e.detail
    if (!value.trim()) {
      this.setData({
        goods:[],
        isFocus:false
      })
      return
    }

    clearTimeout(this.TimeId)
    this.TimeId = setTimeout(() => {
      this.qsearch(value)
      
    }, 1000);
   
  },

  async qsearch(query) {
    console.log(query);
    let res = await request({url:"/goods/qsearch", data:{query}})
 
    res.forEach(v1 => {
      var regex = new RegExp(query);
        v1.goods_name = v1.goods_name.replace(regex, "<font color=red>" +query+ "</font>")
        // v2.navigator_url = v2.navigator_url.replace(/\?/,'/index\?')     
    })
    console.log(res);

    this.setData({
      goods:res,
      isFocus:true
    })
  },

  handleCancel() {
    this.setData({
      inValue:"",
      isFocus: false,
      goods: []
    })
  }

})